package com.map.lbs;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.map.lbs.http.ApiClient;
import com.map.lbs.http.PostDataSuccessEvent;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class UserProfileActivity extends Activity {
	
	private static final int IMAGE_PICK_UP = 3025;
	private ImageView photo;
	private Bitmap photoImage;
	private TextView nickname;
	private TextView city;
	private TextView birthday;
	private AlertDialog inputDialog;
	//private TextView sex;

	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_profile);
		
		photo = (ImageView) findViewById(R.id.user_profile_photo);
		this.registerForContextMenu(photo);
		
		nickname = (TextView) findViewById(R.id.user_profile_nickname);
		nickname.setLongClickable(true);
		nickname.setOnLongClickListener(new OnLongClickListener(){

			@Override
			public boolean onLongClick(View v) {
				final EditText edit = new EditText(UserProfileActivity.this);
				edit.setText(nickname.getText());
				AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
				
				builder.setTitle("请输入名字")
					 .setView(edit)
					 .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						} 
					 })
					 .setPositiveButton("OK", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							nickname.setText(edit.getText());
						}
					 });
				
				inputDialog = builder.create();
				inputDialog.show();
				return true;
			}
			
		});
		
		city = (TextView) findViewById(R.id.user_profile_city);
		city.setLongClickable(true);
		city.setOnLongClickListener(new OnLongClickListener(){

			@Override
			public boolean onLongClick(View v) {
				final EditText edit = new EditText(UserProfileActivity.this);
				edit.setText(city.getText());
				AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
				
				builder.setTitle("请输入所在城市")
					 .setView(edit)
					 .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						} 
					 })
					 .setPositiveButton("OK", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							city.setText(edit.getText());
						}
					 });
				
				inputDialog = builder.create();
				inputDialog.show();
				return true;	
			}
			
		});
		
		birthday = (TextView) findViewById(R.id.user_profile_birthday);
		birthday.setLongClickable(true);
		birthday.setOnLongClickListener(new OnLongClickListener(){

			@Override
			public boolean onLongClick(View v) {
				final EditText edit = new EditText(UserProfileActivity.this);
				edit.setText(birthday.getText());
				AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
				
				builder.setTitle("请输入生日")
					 .setView(edit)
					 .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						} 
					 })
					 .setPositiveButton("OK", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							birthday.setText(edit.getText());
						}
					 });
				
				inputDialog = builder.create();
				inputDialog.show();
				return true;	
			}
			
		});
		
		Button btnSave = (Button) findViewById(R.id.btn_user_profile_save);
		btnSave.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("description", nickname.getText());
				params.put("birthday", birthday.getText());
				params.put("city", city.getText());
				params.put("picture", Base64.encodeToString(
						ImageHelper.getImageBytes(photoImage, 128, 128), Base64.DEFAULT));
				
				ApiClient client = new ApiClient("https://social-map.rhcloud.com/api/user/update");
				client.postData(params, new PostDataSuccessEvent(){

					@Override
					public void handleResponse(InputStream is) {
						
					}
					
				}, null);
				
			}
			
		});
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
		menu.setHeaderTitle("选择头像");
		menu.add(Menu.NONE, Menu.FIRST, 1, "本地图片");
		menu.add(Menu.NONE, Menu.FIRST + 1, 2, "相机拍照");
		super.onCreateContextMenu(menu, v, menuInfo);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item){
		
		Intent intent = null;
		if(item.getItemId() ==  Menu.FIRST){
			intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		}else{
			intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		}
		
		intent.putExtra("crop", "true");
	    intent.putExtra("aspectX", 1);
	    intent.putExtra("aspectY", 1);
	    intent.putExtra("outputX", 128);
	    intent.putExtra("outputY", 128);
	    intent.putExtra("return-data", true);
		startActivityForResult(intent, IMAGE_PICK_UP);
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if(resultCode == RESULT_OK && requestCode == IMAGE_PICK_UP){
	    	photoImage = data.getParcelableExtra("data");
	        photo.setImageBitmap(photoImage);
	    }
	}
}
